﻿using FirstFloor.ModernUI.Presentation;
using FirstFloor.ModernUI.Windows.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using Microsoft.Practices.Unity;
using appCtx = EasyInvoice.ApplicationContext.AppContext;
using System.ComponentModel;

namespace EasyInvoice.WPF.ViewModel
{
    public class LogInViewModel : NotifyPropertyChanged, IDataErrorInfo
    {
        private string _UserName = EasyInvoice.WPF.Properties.Settings.Default.LastAuthenticatedUser;

        private Services.UsersService _serviceUsers;

        private UserViewModel _User;

        public LogInViewModel() { _serviceUsers = appCtx.Instance.IoC.Resolve<Services.UsersService>(); }

        public string UserName
        {
            get
            {
                return this._UserName;
            }
            set
            {
                this._UserName = value;
                this.OnPropertyChanged("UserName");
            }
        }

        private string _localPassword = string.Empty;

        public string Password
        {
            get
            {
                //return this._user.Password;
                return string.Empty;
            }
            set
            {
                this._localPassword = value;
                this.OnPropertyChanged("Password");
            }
        }

        public UserViewModel User
        {
            get
            {
                return this._User;
            }
            private set{}
        }
        //for opening up the Add User window
        private ICommand showLoginCommand;


        public ICommand ShowLoginCommand
        {
            get
            {
                if (showLoginCommand == null)
                {
                    showLoginCommand = new Common.CommandBase(i => this.Authenticate(), null);
                }
                return showLoginCommand;
            }
        }

        private void Authenticate()
        {

            //save user
            try
            {

                //var usr = from ModelCE.User user in new ModelCE.User().GetAllCE()
                //          where user.UserName == _UserName && user.Password == Common.Utils.ConvertToSHA512(
                //                                       string.Format("{0}|{1}", _UserName, _localPassword)
                //                                                       )
                //          select user;
                //EasyInvoice.WPF.Properties.Settings.Default.AuthenticatedUser = (object)new UserViewModel(usr.First());

                var authenticateUser = from Model.User user in this._serviceUsers.GetAll()
                                       where user.UserName == _UserName && user.Password ==
                                            Common.Utils.ConvertToSHA512(string.Format("{0}|{1}", _UserName, _localPassword))
                                       select user;

                EasyInvoice.WPF.Properties.Settings.Default.AuthenticatedUser = (object)new UserViewModel(authenticateUser.First());
                EasyInvoice.WPF.Properties.Settings.Default.LastAuthenticatedUser = ((UserViewModel)EasyInvoice.WPF.Properties.Settings.Default.AuthenticatedUser).UserName;
                EasyInvoice.WPF.Properties.Settings.Default.Save();
                ((MainWindowViewModel)Application.Current.MainWindow.DataContext).SelectedLink = new Uri("/Pages/Introduction.xaml", UriKind.Relative);
                ((MainWindowViewModel)Application.Current.MainWindow.DataContext).RefreshMenus();
                
                
            }
            catch (Exception ex)
            {
                ModernDialog.ShowMessage(ex.Message, "Authentication Failed", MessageBoxButton.OK);
            }
        }

        public string Error
        {
            get { return null; }
        }
        public string this[string columnName]
        {
            get
            {
                if (columnName == "UserName")
                {
                    return string.IsNullOrEmpty(this.UserName) ? "Required value" : null;
                }
                return null;
            }
        }
    }
}
